home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Magazin/MacEasy 1
/
Mac Magazin and MacEasy Magazine CD - Issue 01.iso
/
Sharewarebibliothek
/
Powermac
/
C64
/
SOURCE
/
AppleFloppy.c
< prev
next >
Wrap
Text File
|
1994-06-06
|
7KB
|
321 lines
/*
Commodore 64 Emulator v0.4 Earle F. Philhower III
Copyright (C) 1993-4 (st916w9r@dunx1.ocs.drexel.edu)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <Controls.h>
#include "Processor.h"
#include "Resources.h"
#include "Error.h"
#include "FileTypes.h"
typedef struct {
long size;
byte name[24];
unsigned short type;
} CommieEntry;
static WindowPtr dirWind, updt;
static ControlHandle dirScroll;
static CommieEntry dirEntry[144];
static int dirEntries;
static byte header[17], id[3];
static char typeStr[5][4]={"\pUNK", "\pSEQ", "\pPRG", "\pUSR", "\pREL"};
#ifdef __MWERKS__
static ControlActionUPP myCntlAction = nil;
#endif
static int CollectEntries(CommieEntry entry[]);
static void Xlate2P(byte *in, byte *out);
pascal void DirScrollProc(ControlHandle ctl, int code);
int DirInitialize(void)
{
Rect scrollRect;
/* Read in the directory listing window */
dirWind=GetNewWindow(kDirList, nil, (WindowPtr)-1L);
if (dirWind==nil) return kMissingResource;
SetWRefCon(dirWind, kDirWindow);
/* Add the scroll bar on the right side */
scrollRect=dirWind->portRect;
scrollRect.top--;
scrollRect.bottom++;
scrollRect.left=scrollRect.right-16+1;
scrollRect.right++;
dirScroll=NewControl(dirWind, &scrollRect, "\p", TRUE, 1, 1, 1,
scrollBarProc, 0L);
#ifdef __MWERKS__
myCntlAction = NewControlActionProc( DirScrollProc );
#endif
return kNoError;
}
void FloppyDirectory(void)
{
dirEntries=CollectEntries(dirEntry);
if (dirEntries==0) return;
SetWTitle(dirWind, header);
SetCtlMax(dirScroll, dirEntries);
SetCtlValue(dirScroll, 1);
SelectWindow(dirWind);
ShowWindow(dirWind);
}
void RedrawDir(void)
{
int x, width, sizeWidth, nameWidth, spaceWidth, bot, start;
Str255 temp;
SetPort(dirWind);
EraseRect(&dirWind->portRect);
DrawControls(dirWind);
TextFont(monaco);
TextSize(9);
sizeWidth=StringWidth("\p 999");
nameWidth=StringWidth("\pXXXXXXXXXXXXXXXXXX");
spaceWidth=StringWidth("\p ");
for (x=GetCtlValue(dirScroll)-1, start=0; x<dirEntries; x++, start++)
{
bot=start*12+12;
NumToString(dirEntry[x].size, temp);
width=StringWidth(temp);
MoveTo(4+sizeWidth-width, bot);
DrawString(temp);
MoveTo(4+sizeWidth+spaceWidth, bot);
DrawString(dirEntry[x].name);
MoveTo(4+sizeWidth+2*spaceWidth+nameWidth, bot);
DrawString((unsigned char *)typeStr[dirEntry[x].type]);
}
}
static int CollectEntries(CommieEntry commie[])
{
byte buff[256], *item;
int entry, i;
ExternalRead(buff, 18, 0);
Xlate2P(buff+0x90, header);
id[0]=2;
id[1]=buff[0xa2];
id[2]=buff[0xa3];
entry=0;
while ((buff[0])&&(entry<144))
{
if (ExternalRead(buff, buff[0], buff[1])!=0) return 0;
for (i=0; i<8; i++)
{
item=&buff[i*32];
if (item[0x02]&128)
{
commie[entry].size=item[0x1e]+(((word)item[0x1f])<<8);
Xlate2P(item+0x05, commie[entry].name);
switch (item[0x02])
{
case 129:
commie[entry].type=1;
break;
case 130:
commie[entry].type=2;
break;
case 131:
commie[entry].type=3;
break;
case 132:
commie[entry].type=4;
break;
default:
commie[entry].type=0;
break;
}
entry++;
}
}
}
return entry;
}
static void Xlate2P(byte *in, byte *out)
{
int i;
i=0;
out[1]='"';
while (in[i]!=0xa0)
{
out[i+2]=in[i];
i++;
if (i>=16) break;
}
out[i+2]='"';
out[0]=i+2;
}
pascal void DirScrollProc(ControlHandle ctl, int code)
{
int cur, max, min;
RgnHandle rgn, clip;
Rect rect;
max=GetCtlMax(ctl);
min=GetCtlMin(ctl);
cur=GetCtlValue(ctl);
switch (code)
{
case inPageDown:
case inDownButton:
if (cur<max)
{
cur++;
rect=dirWind->portRect;
rect.right-=16;
rgn=NewRgn();
clip=NewRgn();
GetClip(clip);
ScrollRect(&rect, 0, -12, rgn);
SetCtlValue(ctl, cur);
SetClip(rgn);
RedrawDir();
SetClip(clip);
DisposeRgn(rgn);
DisposeRgn(clip);
}
break;
case inPageUp:
case inUpButton:
if (cur>min)
{
cur--;
rect=dirWind->portRect;
rect.right-=16;
rgn=NewRgn();
clip=NewRgn();
GetClip(clip);
ScrollRect(&rect, 0, +12, rgn);
SetCtlValue(ctl, cur);
SetClip(rgn);
RedrawDir();
SetClip(clip);
DisposeRgn(rgn);
DisposeRgn(clip);
}
break;
}
}
#define sq(x) ((x)*(x))
void FloppyClick(EventRecord evt)
{
Point pt;
short part;
ControlHandle ctl;
int which;
static long lastClickTime;
static Point lastClickPoint;
pt=evt.where;
SetPort(dirWind);
GlobalToLocal(&pt);
part=FindControl(pt, dirWind, &ctl);
if (part==0)
{
if (((evt.when-lastClickTime)>GetDblTime()) ||
((sq(lastClickPoint.h-evt.where.h)+sq(lastClickPoint.v-evt.where.v))
>4))
{
lastClickTime=evt.when;
lastClickPoint=evt.where;
return;
}
which=pt.v/12;
which+=GetCtlValue(dirScroll)-1;
if (which<dirEntries)
{
StandardFileReply reply;
byte buff[17], *name, data;
int x;
long size; short fNum;
byte Read1541(), Open1541();
name=dirEntry[which].name;
for (x=1; x<(name[0]-1); x++) buff[x]=PetConv(name[x+1]);
buff[0]=name[0]-2;
StandardPutFile("\pSave File As:", buff, &reply);
if (reply.sfGood)
{
FSpCreate(&reply.sfFile, (OSType)APPLTYPE, (OSType)HDFTYPE, 0);
FSpOpenDF(&reply.sfFile, fsRdWrPerm, &fNum);
name=dirEntry[which].name;
for (x=1; x<(name[0]-1); x++) buff[x]=name[x+1];
buff[0]=name[0]-2;
if (Open1541((char *)(&buff[1]), (int)buff[0], 0)!=0) return;
size=1;
while(Read1541(&data, 0)==0) FSWrite(fNum, &size, &data);
FSClose(fNum);
Close1541(0);
}
}
else
{
SysBeep(1);
return;
}
return;
}
if (part==inThumb)
{
TrackControl(dirScroll, pt, nil);
RedrawDir();
}
#ifndef __MWERKS__
else TrackControl(dirScroll, pt, &DirScrollProc);
#else
else TrackControl(dirScroll, pt, myCntlAction);
#endif
}
void FloppyAlert(Str255 str)
{
/* Store that string in our dialog ParamText (^0) */
if (str[0]!=0) ParamText(str, "\p", "\p", "\p");
else ParamText("\pUnknown error condition.", "\p", "\p", "\p");
/* Display the alert. When user clicks OK return */
Alert(kFloppyAlert, nil);
}